<template>
	<view class="facilityDetail-container">
		<view class="header">
			<view class="title">
				{{jobSubmitResultVo.job.subject}}
			</view>
			<view class="bottom">
				<view class="info">
					<view class="overhaulDate">
						<text class="iconfont icon-icon-test5" style="margin-right: 10rpx;"></text>
						{{jobSubmitResultVo.job.startTime}} 至 {{jobSubmitResultVo.job.endTime}}
					</view>
					<view class="useStatus used" v-if="jobSubmitResultVo.jobSubmit.status == 'a'">
						<text class="iconfont icon-icon-test5" style="margin-right: 10rpx;"></text>
						待审批
					</view>
					<view class="useStatus used" v-if="jobSubmitResultVo.jobSubmit.status == 'b'">
						<text class="iconfont icon-icon-test5" style="margin-right: 10rpx;"></text>
						已审批
					</view>
					<view class="useStatus notUsed" v-if="jobSubmitResultVo.jobSubmit.status == 'c'">
						<text class="iconfont icon-icon-test5" style="margin-right: 10rpx;"></text>
						未提交
					</view>
				</view>
				<view class="titleJob">
					作业
					<u-toast ref="uToast"></u-toast>
				</view>
				<view class="introduction">
					作业内容：{{jobSubmitResultVo.job.content}}
					<view style="display: flex; flex-wrap: wrap; justify-content: space-between;">
						<view v-for="(img,index) in jobSubmitResultVo.job.jobPhotos">
							<u-image :src="img.url" style="margin:  20rpx 0rpx;" width="200upx" height="200upx" mode=""
								@click="imgPreview(jobSubmitResultVo.job.jobPhotos)">
							</u-image>
						</view>
					</view>
				</view>
			</view>
			<view><br /></view>
			<view class="titleSubmitJob">
				我的
			</view>
			<view class="bottom">
				<view class="introduction">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" :maxCount="10">
					</u-upload>
				</view>
			</view>
			<view class="footer">
				<view class="left">
					温馨提示：认真检查作业
				</view>
				<view class="right" @click="submit()">
					<text class="iconfont icon-yuyue1" style="margin-right: 20rpx;"></text>
					确认提交
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				jobSubmitResultVo: "",
				fileList1: [],
				token: '',
				imgs: [],
				jobSubmit: {
					id: "",
					jobSubmitPhotos: [],
					studentId: ""
				},
				flag: false
			}
		},
		onLoad(opt) {
			// console.log(opt.id)
			this.token = {
				token: uni.getStorageSync("authentication")
			};
			this.id = opt.id
			this.jobSubmit.id = opt.id
			this.jobSubmit.studentId = uni.getStorageSync('studentId');
			this.getJobSubmit();

		},
		methods: {
			getJobSubmit() {
				this.$myReq({
						url: `/job/api/jobSubmit/detail/` + this.id,
						method: 'get'
					})
					.then((res) => {
						console.log(res.data)
						this.jobSubmitResultVo = res.data
						this.fileList1 = res.data.jobSubmit.jobSubmitPhotos
						for (var i = 0; i < res.data.jobSubmit.jobSubmitPhotos.length; i++) {
							let img = {
								url: res.data.jobSubmit.jobSubmitPhotos[i].url
							}
							this.jobSubmit.jobSubmitPhotos.push(img);
						}
						console.log(this.jobSubmit.jobSubmitPhotos)
						if (res.data.jobSubmit.jobSubmitPhotos.length == 0) {
							this.flag = true;
						}
					})
					.catch((err) => {
						console.log(err)
					})
			},
			imgPreview(imgs) {
				console.log("查看大图", imgs)
				let urls = [];
				for (let i = 0; i < imgs.length; i++) {
					urls.push(imgs[i].url);
				};
				uni.previewImage({
					indicator: "number",
					loop: true,
					urls: urls
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.jobSubmit.jobSubmitPhotos.splice(event.index, 1)
				console.log(this.jobSubmit.jobSubmitPhotos)
			},
			// 新增图片
			async afterRead(event) {
				console.log(event);
				event.url = this.img;
				console.log(event)
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.12.95:8080/job/api/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: this.token,
						success: (res) => {
							console.log(res.data);
							var testJson = res.data;
							testJson = eval("(" + testJson + ")");
							console.log(testJson.data);
							let img = {
								url: testJson.data
							}
							this.jobSubmit.jobSubmitPhotos.push(img);
							console.log(this.jobSubmit.jobSubmitPhotos)
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},
			submit() {
				if (this.flag == false) {
					this.$myReq({
							url: `/job/api/jobSubmit/update/`,
							method: 'post',
							data: this.jobSubmit
						})
						.then((res) => {
							console.log(res);
							if (res.code == 200) {
								this.showToast({
									type: 'success',
									title: '修改成功',
									message: "已成功修改此作业",
									iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
								})
								uni.navigateTo({
									url: '/pages/Job/job'
								});
							}
						})
						.catch((err) => {
							console.log(err)
						})
				} else {
					this.$myReq({
							url: `/job/api/jobSubmit/add/`,
							method: 'post',
							data: this.jobSubmit
						})
						.then((res) => {
							console.log(res);
							if (res.code == 200) {
								this.showToast({
									type: 'success',
									title: '提交成功',
									message: "已成功提交此作业",
									iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
									url: '/pages/job/Job',
								})
								uni.navigateTo({
									url: '/pages/Job/job'
								});

							}
						})
						.catch((err) => {
							console.log(err)
						})
				}

			}
		}
	}
</script>

<style lang="scss">
	.header {
		padding: 20rpx 40rpx 0rpx 40rpx;

		.title {
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.titleJob {
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
			color: #e6ba90;
		}

		.titleSubmitJob {
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
			color: #00aa00;
		}

		.bottom {
			.info {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				margin-bottom: 30rpx;
				padding-bottom: 30rpx;
				border-bottom: 2px solid $main-garyA;

				.overhaulDate {
					// border: 1px solid;
					height: 60rpx;
					line-height: 60rpx;
				}

				.useStatus {
					border: 1px solid;
					height: 60rpx;
					width: 200rpx;
					line-height: 60rpx;
					text-align: center;
					// margin-right: 30rpx;
					color: white;
					font-size: 27rpx;
					border-radius: 30rpx;
				}

				.used {
					background-color: #09BE4F;
				}

				.notUsed {
					background-color: #09BE4F;
				}
			}

			.introduction {
				// border: 1px solid red;
				padding: 30rpx 25rpx 30rpx 25rpx;
				border-radius: 30rpx;
				background-color: $main-garyA;
				color: #000000;
				box-shadow: 0px 0px 3px 1px $main-garyB;
				font-size: 30rpx;
				line-height: 50rpx;
			}
		}
	}

	.content {
		padding: 30rpx 40rpx 0rpx 40rpx;

		.line {
			border-bottom: 2px solid $main-garyA;
			margin-bottom: 20rpx;
		}

		.timeItemRow {
			// border: 1px solid red;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
			height: 170rpx;
			margin-bottom: 50rpx;
			align-items: center;

			.timeItem {
				// border: 1px solid #cbcbcb;
				border-radius: 30rpx;
				width: 200rpx;
				height: 125rpx;
				align-items: center;
				padding-top: 45rpx;

				.time {
					// border: 1px solid blue;
					margin-bottom: 15rpx;
					text-align: center;
					font-size: 27rpx;
					font-weight: bold;
				}

				.orderState {
					// border: 1px solid blue;
					// font-weight: bold;
					text-align: center;
				}
			}
		}
	}

	.footer {
		padding: 0rpx 40rpx 0rpx 40rpx;
		position: fixed;
		bottom: 10rpx;
		// border: 1px solid red;
		width: 670rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		// height: 100rpx;
		.left {
			font-size: 30rpx;
			line-height: 50rpx;
			width: 360rpx;
			border-right: 2px solid #cecece;
			// border: 1px solid black;
		}

		.right {
			float: right;
			// border: 1px solid;
			height: 100rpx;
			width: 250rpx;
			text-align: center;
			line-height: 100rpx;
			color: white;
			background-color: #09BE4F;
			border-radius: 10rpx;
		}
	}

	.active {
		color: #FF2B2B;
		border: 1px solid #FF2B2B;
		// box-shadow: 0rpx 0rpx 1rpx 1rpx #FF2B2B;
	}

	.notActive {
		color: #000000;
		border: 1px solid #cbcbcb;
	}
</style>
